
#include "mem_manage.h"
//#include "writefortest.h"
#include <assert.h>
#include "read_shmem_test.h"

#define DEBUG


mailinfo_t *read_shmem()
{
	int mailcount;
	int readsize = 0;
	char * mailtmpsave = NULL;
	int semid;
	void * smptr;
	u_int mailsize = 0;
	u_char * head;

	int rest = -1;
	u_int * uiptr = NULL;
	s_int * siptr = NULL;
	int writesize = 0;
	int sublen = 0;
	int textlen = 0;
    char * testptr = NULL;
    mailheadinfo_t * mailhead = NULL;
    mailinfo_t *mailinfo = NULL;

	
    semid=getSem(S_SEM_NAME,S_SEM_ID,GET);               //共享内存id号
	mem_start=getMem(S_MEM_NAME,S_MEM_ID,TOTAL_LEN,GET); //打开共享内存
	smptr=mem_start;                                     //共享内存链接

	printf("Read for read sub and text\n");
	mailhead = (mailheadinfo_t *)malloc(sizeof(mailheadinfo_t));
	mailinfo = (mailinfo_t *)malloc(sizeof(mailinfo_t));
	assert(mailhead != NULL);
	//memset(mailhead,0,sizeof(mailinfo));
	
	//读取一封邮件的头部信息和正文信息
	for(;;) //读第一次
	{
    	memset(mailhead,0,sizeof(mailheadinfo_t));
        readsize = my_read(&smptr,mailhead,semid);  //读取首部信息
        //循环等待读取首部信息
        if(readsize == READ_FAILED)
        {
#ifdef DEBUG
            printf("read wait~\n");
#endif
            sleep(1);
        }
        else if(readsize == 0)
        {
#ifdef DEBUG
            printf("read 0 char~\n");
#endif
            sleep(1);
        }
        else
        {
           printf("\n----------------Mail head start----------------\n");
           printf("readsize: %d\n",readsize);
           printf("sortip %u| desip %u\n | sorport %u | desport %u\n",mailhead->sorip,mailhead->desip,mailhead->sorport,mailhead->desport);
           printf("maisize is %u | res is %u\n",mailhead->mailsize, mailhead->mailres);
           mailsize =(int)mailhead->mailsize;
           printf("\n----------------Mail head end---------------------------\n");
	       break;
        }
    }
	mailinfo ->mailheadptr =(mailheadinfo_t *)malloc(sizeof(mailheadinfo_t));
	mailinfo ->mailheadptr = mailhead;
		
		
    for(; ;)    //读第二次
    {
       mailtmpsave = NULL;
       mailtmpsave = (char *)malloc((mailsize) * sizeof(char)+1);
       assert(mailtmpsave!=NULL);
       memset(mailtmpsave,0,mailsize);
       readsize = my_read(&smptr,mailtmpsave,semid);     
       if(readsize == READ_FAILED)
       {
           printf("read mail fail then sleep\n");
           free(mailtmpsave);
           sleep(1);
       }
       else if(readsize == 0)
       {
            printf("read 0 char of mailcontent~\n");
            free(mailtmpsave);
            sleep(1);
        }
        else  
        {
            printf("readsize %d\n", readsize );
            siptr = (s_int *)mailtmpsave;
            sublen = (int)*siptr;
            printf("sublen is %d\n",sublen);
              
            siptr = (s_int *)(mailtmpsave+2);
            textlen = (int)*siptr;
            printf("textlen is %d\n",textlen);  

            mailinfo ->mailsubptr = (char *)malloc(sizeof(char)*sublen);
            assert(mailinfo ->mailsubptr != NULL);
            strncpy(mailinfo ->mailsubptr,mailtmpsave+4,sublen);
               
            mailinfo ->mailtextptr = (char *)malloc(sizeof(char)*textlen);
            assert(mailinfo ->mailtextptr != NULL);
            strncpy(mailinfo ->mailtextptr,mailtmpsave+4+sublen,textlen);
               
			free(mailtmpsave);
            break;
         } //end else
    }   //end for
    #if 0
    printf("mailinfo->mailheadptr->sorip is %u\n",mailinfo->mailheadptr->sorip);
   	printf("mailinfo->mailheadptr->desip is %u\n",mailinfo->mailheadptr->desip);
   	printf("mailinfo->mailheadptr->sorport is %u\n",mailinfo->mailheadptr->sorport);
   	printf("mailinfo->mailheadptr->destport is %u\n",mailinfo->mailheadptr->desport);
   	printf("mailsub is %s\n",mailinfo->mailsubptr);
   	printf("mailtext is %s\n",mailinfo->mailtextptr);
   	printf("mailinfo->mailheadptr->mailsize is %u\n",mailinfo->mailheadptr->mailsize );
    printf("mailinfo->mailheadptr->mailres is %u\n", mailinfo->mailheadptr->mailres);
    #endif
    printf("the %d mailtext is OK\n",mailcount);
        
       
     // end for
    
    //free(mailhead);
    //free(mailinfo->mailheadptr);
    //free(mailinfo->mailsubptr);
    //free(mailinfo->mailtextptr);
    //free(mailinfo);
   return mailinfo;//返回指向邮件的指针
}


